﻿#include "MWindow.h"
#include "ui_MWindow.h"
#include "mainwindow.h"
#include "DEM/FormTileDEM.h"
#include "IMAGE/FormTileImage.h"
#include <QSplitter>

#include <gdal_priv.h>
#include <SignalRegistry.h>
#include <QDateTime>

MWindow::MWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MWindow)
{
    ui->setupUi(this);


    connect(ui->actionTileDEM,SIGNAL(triggered(bool)),this,SLOT(slotTileDEM()));
    connect(ui->actionTileImg,SIGNAL(triggered(bool)),this,SLOT(slotTileImage()));
    connect(SignalRegistry::instance(), SIGNAL(signalShowMessage(const QString&)), this, SLOT(slotShowOutputMessage(const QString&)));
    connect(SignalRegistry::instance(), SIGNAL(signalShowProcess(float, const QString&)), this, SLOT(slotShowOutputProcess(float,const QString&)));

    GDALAllRegister();//注册驱动
    CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");//支持中文路径

    ui->verticalLayout->removeWidget(ui->mdiArea);
    ui->verticalLayout->removeWidget(ui->groupBox);
    QSplitter *splitter = new QSplitter(Qt::Vertical);
    splitter->addWidget(ui->mdiArea);
    splitter->addWidget(ui->groupBox);
    ui->verticalLayout->addWidget(splitter);
    splitter->setStretchFactor(5, 1);
    //splitter->setStretchFactor(1, 1);
}

MWindow::~MWindow()
{
    delete ui;
}

void MWindow::slotTileDEM()
{
    FormTileDEM* mw = new FormTileDEM();
    ui->mdiArea->addSubWindow(mw);
    mw->showMaximized();
}

void MWindow::slotTileImage()
{
    FormTileImage* mw = new FormTileImage();
    ui->mdiArea->addSubWindow(mw);
    mw->showMaximized();
}

void MWindow::slotShowOutputMessage(const QString& msg)
{
    if(ui->textEdit->document()->lineCount() > 500){
        ui->textEdit->document()->clear();
    }
    QDateTime dt = QDateTime::currentDateTime();
    ui->textEdit->append(dt.toString("[yyyy-MM-dd hh:mm:ss] ") + msg);
    ui->textEdit->moveCursor(QTextCursor::End);
}

void MWindow::slotShowOutputProcess(float p, const QString& msg)
{
    if(ui->textEdit->document()->lineCount() > 500){
        ui->textEdit->document()->clear();
    }
    int ip = p + 0.5;
    QString tmp = QString::fromLocal8Bit("%1% %2");
    tmp = tmp.arg(ip).arg(msg);
    QDateTime dt = QDateTime::currentDateTime();
    ui->textEdit->append(dt.toString("[yyyy-MM-dd hh:mm:ss] ") + tmp);
    ui->textEdit->moveCursor(QTextCursor::End);
}
